# This file should only be included when WIN32

SET(LIB_NAME OpenThreads)
SET(LIB_PUBLIC_HEADERS ${OpenThreads_PUBLIC_HEADERS})

ADD_DEFINITIONS(-DWIN32_LEAN_AND_MEAN)
IF(NOT OSG_DETERMINE_WIN_VERSION)
    ADD_DEFINITIONS(-D_WIN32_WINNT=0x0400)
ENDIF(NOT OSG_DETERMINE_WIN_VERSION)
SOURCE_GROUP("Header Files" FILES ${LIB_PUBLIC_HEADERS})
SET_SOURCE_FILES_PROPERTIES(${LIB_PUBLIC_HEADERS} PROPERTIES HEADER_FILE_ONLY ON)

ADD_LIBRARY(${LIB_NAME}
    ${OPENTHREADS_USER_DEFINED_DYNAMIC_OR_STATIC}
    ${LIB_PUBLIC_HEADERS}
    HandleHolder.h
    Win32BarrierPrivateData.h
    WIN32Condition.cpp
    Win32ConditionPrivateData.h
    Win32Mutex.cpp
    Win32MutexPrivateData.h
    Win32Thread.cpp
    Win32ThreadBarrier.cpp
    Win32ThreadPrivateData.h
    ../common/Version.cpp
    ../common/Atomic.cpp
    ${OPENTHREADS_VERSIONINFO_RC}
)

SET_TARGET_PROPERTIES(${LIB_NAME} PROPERTIES FOLDER "OSG Core")
IF(OPENTHREADS_SONAMES)
  SET_TARGET_PROPERTIES(${LIB_NAME} PROPERTIES VERSION ${OPENTHREADS_VERSION} SOVERSION ${OPENTHREADS_SOVERSION})
ENDIF()

# Since we're building different platforms binaries in 
# their respective directories, we need to set the 
# link directory so it can find this location.
LINK_DIRECTORIES(
    ${CMAKE_CURRENT_BINARY_DIR}
)

IF(MSVC AND OSG_MSVC_VERSIONED_DLL)
    HANDLE_MSVC_DLL(ot ${OPENTHREADS_SOVERSION})
ENDIF()

SET(INSTALL_BINDIR bin)
IF(OSG_EXPORT_TARGETS)

    INSTALL(
        TARGETS OpenThreads
        EXPORT  OpenThreads
        ARCHIVE DESTINATION lib COMPONENT libopenthreads-dev
        LIBRARY DESTINATION lib COMPONENT libopenthreads
        RUNTIME DESTINATION ${INSTALL_BINDIR} COMPONENT libopenthreads
    )

    INSTALL(
        EXPORT OpenThreads
        NAMESPACE ${PKG_NAMESPACE}::
        DESTINATION ${INSTALL_CONFIGDIR}
        FILE OpenThreads-targets.cmake
        COMPONENT libopenthreads-dev
    )

    # Intentionally blank
    SET(COMPONENT_CMAKE_DEPENDENCIES)

    SET(TARGET_IMPORT_INPUT_FILE  "${PROJECT_SOURCE_DIR}/packaging/cmake/openscenegraph-component-import-targets.cmake")
    SET(TARGET_IMPORT_OUTPUT_FILE "${PROJECT_BINARY_DIR}/packaging/cmake/openscenegraph-OpenThreads-import-targets.cmake")
    CONFIGURE_FILE(
        ${TARGET_IMPORT_INPUT_FILE}
        ${TARGET_IMPORT_OUTPUT_FILE}
        @ONLY
    )
    INSTALL(
        FILES ${TARGET_IMPORT_OUTPUT_FILE}
        DESTINATION ${INSTALL_CONFIGDIR}
        COMPONENT libopenscenegraph-dev
    )

ELSE()

    INSTALL(
        TARGETS OpenThreads
        ARCHIVE DESTINATION lib COMPONENT libopenthreads-dev
        LIBRARY DESTINATION lib COMPONENT libopenthreads
        RUNTIME DESTINATION ${INSTALL_BINDIR} COMPONENT libopenthreads
    )

ENDIF()
INSTALL(
    FILES ${OpenThreads_PUBLIC_HEADERS}
    DESTINATION include/OpenThreads
    COMPONENT libopenthreads-dev
)

IF(MSVC AND DYNAMIC_OPENTHREADS)
    GET_TARGET_PROPERTY(PREFIX ${LIB_NAME} PREFIX)
    IF( ${CMAKE_GENERATOR} STREQUAL "Ninja" )
	INSTALL(FILES ${CMAKE_CURRENT_BINARY_DIR}/${PREFIX}${LIB_NAME}${CMAKE_RELEASE_POSTFIX}.pdb DESTINATION ${INSTALL_BINDIR} COMPONENT libopenthreads CONFIGURATIONS Release)
	INSTALL(FILES ${CMAKE_CURRENT_BINARY_DIR}/${PREFIX}${LIB_NAME}${CMAKE_RELWITHDEBINFO_POSTFIX}.pdb DESTINATION ${INSTALL_BINDIR} COMPONENT libopenthreads CONFIGURATIONS RelWithDebInfo)
	INSTALL(FILES ${CMAKE_CURRENT_BINARY_DIR}/${PREFIX}${LIB_NAME}${CMAKE_DEBUG_POSTFIX}.pdb DESTINATION ${INSTALL_BINDIR} COMPONENT libopenthreads CONFIGURATIONS Debug)
    ELSE( ${CMAKE_GENERATOR} STREQUAL "Ninja" )
	INSTALL(FILES ${OUTPUT_BINDIR}/${PREFIX}${LIB_NAME}${CMAKE_RELWITHDEBINFO_POSTFIX}.pdb DESTINATION ${INSTALL_BINDIR} COMPONENT libopenthreads CONFIGURATIONS RelWithDebInfo)
	INSTALL(FILES ${OUTPUT_BINDIR}/${PREFIX}${LIB_NAME}${CMAKE_DEBUG_POSTFIX}.pdb DESTINATION ${INSTALL_BINDIR} COMPONENT libopenthreads CONFIGURATIONS Debug)
    ENDIF( ${CMAKE_GENERATOR} STREQUAL "Ninja" )
ENDIF(MSVC AND DYNAMIC_OPENTHREADS)

#commented out# INCLUDE(ModuleInstall OPTIONAL)
